package GUI;

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

import javax.swing.AbstractAction;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.KeyStroke;
import javax.swing.border.EmptyBorder;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.JTextField;

import Other.Glob;

import enums.SearchType;

public class ShipmentSearchOption extends JFrame {

	private JPanel contentPane;
	private JTextField tfCode;
	boolean disable;
	private static ShipmentSearchOption shipmentSearchOptionInstance = null;
	JFrame thisFrame = this;


	/**
	 * Create the frame.
	 */
	private ShipmentSearchOption() {
		setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
		setBounds(100, 100, 336, 189);
		setTitle("Search Shipment");
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		
		JPanel panel = new JPanel();
		GroupLayout gl_contentPane = new GroupLayout(contentPane);
		gl_contentPane.setHorizontalGroup(
			gl_contentPane.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_contentPane.createSequentialGroup()
					.addComponent(panel, GroupLayout.PREFERRED_SIZE, 312, GroupLayout.PREFERRED_SIZE)
					.addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
		);
		gl_contentPane.setVerticalGroup(
			gl_contentPane.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_contentPane.createSequentialGroup()
					.addComponent(panel, GroupLayout.PREFERRED_SIZE, 143, GroupLayout.PREFERRED_SIZE)
					.addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
		);
		
		JLabel lblByClient = new JLabel("1. By Client");
		
		
		JLabel lblByAddresse = new JLabel("2. By Addressee");
		
		JLabel lblChooseOption = new JLabel("Choose Option");
		
		tfCode = new JTextField();
		
		tfCode.getInputMap(tfCode.WHEN_IN_FOCUSED_WINDOW)
		.put(KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), "");
		tfCode.getActionMap().put("", new AbstractAction() {
			 public void actionPerformed(ActionEvent e) {
				if (disable == false){
				//escape from the screen
					escape();
				}
			    }
		});
		tfCode.addKeyListener(new KeyAdapter() {
			@Override
			public void keyTyped(KeyEvent e) {
				//when a number is entered
				if(e.getKeyChar() == KeyEvent.VK_ENTER){
					System.out.println("This is the data type in report when I presse the button" + Glob.type);
					
					String option = tfCode.getText();
					if(disable == false){
					if(option.equals("1") || option.equals("2")) {
					searchShipment();
					}else {
						JOptionPane.showMessageDialog(null, "Enter a valid option from the screen");
						tfCode.setText("");
					}
					}
					
					
				}
			}
		});
		
		tfCode.setColumns(10);
		GroupLayout gl_panel = new GroupLayout(panel);
		gl_panel.setHorizontalGroup(
			gl_panel.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panel.createSequentialGroup()
					.addContainerGap()
					.addGroup(gl_panel.createParallelGroup(Alignment.LEADING)
						.addComponent(lblByClient)
						.addComponent(lblByAddresse)
						.addGroup(gl_panel.createSequentialGroup()
							.addComponent(lblChooseOption, GroupLayout.PREFERRED_SIZE, 96, GroupLayout.PREFERRED_SIZE)
							.addPreferredGap(ComponentPlacement.RELATED)
							.addComponent(tfCode, GroupLayout.PREFERRED_SIZE, 111, GroupLayout.PREFERRED_SIZE)))
					.addContainerGap(109, Short.MAX_VALUE))
		);
		gl_panel.setVerticalGroup(
			gl_panel.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panel.createSequentialGroup()
					.addContainerGap()
					.addComponent(lblByClient)
					.addPreferredGap(ComponentPlacement.RELATED)
					.addComponent(lblByAddresse)
					.addPreferredGap(ComponentPlacement.RELATED, 73, Short.MAX_VALUE)
					.addGroup(gl_panel.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblChooseOption)
						.addComponent(tfCode, GroupLayout.PREFERRED_SIZE, 22, GroupLayout.PREFERRED_SIZE))
					.addContainerGap())
		);
		panel.setLayout(gl_panel);
		contentPane.setLayout(gl_contentPane);
	}
	
	public void escape(){
		
		
	 	ShipmentSearchOption so = ShipmentSearchOption.getInstance();
	 	so.dispose();
	 	
	 	switch(Glob.type) {
	 	
	 	case SHIPMENT:
	 		Shipment s = Shipment.getInstance();
	 		s.enableShipment();
	 		break;
	 		
	 	case ADDSHIPMENTSHIPMENT:
	 		
	 		AddShipment shipment = AddShipment.getInstance();
	 		shipment.enableAddShipment();
	 		break;
	 	}
	 	
		
		
	 	tfCode.setText("");
		
 }
	
	public void disableShipmentSearchOption(){
		
		ShipmentSearchOption.getInstance().disable();
		disable = true;
	
		
	}
	
	
	//method to enable the screen
	public void enableShipmentSearchOption(){
		ShipmentSearchOption.getInstance().enable();
		disable = false;
		tfCode.setText("");
		tfCode.requestFocus();
		ShipmentSearchOption.getInstance().toFront();
	}
	public void searchShipment(){
	    //Glob.type = SearchType.SHIPMENT;
		SearchWindow s = SearchWindow.getInstance();
		s.setTitle();
		s.setVisible(true);
		disableShipmentSearchOption();
		
	}
	
	//singleton
	public static ShipmentSearchOption getInstance(){
		if(shipmentSearchOptionInstance == null){
			shipmentSearchOptionInstance = new ShipmentSearchOption();
		}
		return shipmentSearchOptionInstance;
		}
	
	public String getFieldCode() {
	    return tfCode.getText();
	 }
	
}
